<?php
namespace App\Http\Controllers\Api\v1\User;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Input;

use App\Models\Api\User;

use DB;
// 用户中心
class UserinfoController extends BaseController
{
    public function __construct()
    {
        //echo '000000';exit;
        // parent::filter_xss();
        // if(!defined('IS_ACCESS_TOKEN_CHECKED'))
        // {
        //     parent::__construct();// 接口调用验证
        // }
    }
    /**
     * 获取用户信息
     * @return json
     */
    public function get()
    {
        $id = $request->uid;

        $user =User::where(['id'=>$id])->first(['id','tel','nickname','avatar','forbidden','uc_id','sex','has_self_recode'])->toArray();
        if(empty($user))
        {
            return response()->json(['code'=>10208,'msg'=>'User is not exist.']);
        }
        else
        {
            return response()->json(['code'=>200,'data'=>$user]);
        }
    }
    /**
     * 禁用用户
     * @return json
     */
    public function denied_user()
    {
        $id = $request->uid;

        $result = User::where(['id'=>$id])->save(['forbidden'=>1]);

        if(!$result)
        {
            return response()->json(['code'=>10209,'msg'=>'Failed to denied user.']);
        }else{
            return response()->json(['code'=>200,'msg'=>'Success to denied user.']);
        }
    }

    /**
     * 取消首次登陆标记
     */
    public function not_first_login()
    {
        $id = $request->uid;

        $result = User::where(['id'=>$id])->save(['is_first_login'=>0]);

        if(!$result)
        {
            return response()->json(['code'=>10210,'msg'=>'Failed to do it.']);
        }else{
            return response()->json(['code'=>200,'msg'=>'Success to do it.']);
        }
    }

    /**
     * 更新用户信息
     */
    public function edit()
    {
        $data['id'] = intval(Input::get('id'));
        if(!$data['id'] || !$data['uc_id'] || strlen($data['uc_id']) != 32)
        {
            return response()->json(['code'=>10211,'msg' => 'User is not exist.']);
        }
//        $data['username'] = htmlspecialchars(input('username'));
//        $data['password'] = htmlspecialchars(input('password'));
//        if($data['password'] && strlen($data['password']) != 32)
//        {
//            return json(['code'=>10042,'msg' => 'User is not exist.']);
//        }
        $data['nickname'] = htmlspecialchars(Input::get('nickname'));
        $data['sex'] = intval(Input::get('sex'));
        $data['avatar'] = htmlspecialchars(Input::get('avatar'));
//        $data['email'] = htmlspecialchars(input('email'));
        $data['birthday'] = intval(Input::get('birthday'));
        $data['province'] = htmlspecialchars(Input::get('province'));
        $data['city'] = htmlspecialchars(Input::get('city'));
//        $data['job_id'] = htmlspecialchars(input('job_id'));
        $data['url_token'] = htmlspecialchars(Input::get('url_token'));
        $data['default_timezone'] = htmlspecialchars(Input::get('default_timezone'));

        $result = User::where(['id'=>$request->uid])->update($data);

        if(!$result)
        {
            return response()->json(['code'=>10212,'msg'=>'Failed to do it.']);
        }
        else
        {
            return response()->json(['code'=>200,'msg'=>'Success to do it.']);
        }
    }

    public function save_sex()
    {
//        $data['id'] = intval(input('id'));

        $data['sex'] = intval(Input::get('sex'));
        $result = User::where(['id'=>$request->uid])->update($data);
        if(!$result)
        {
            return response()->json(['code'=>10212,'msg'=>'Failed to do it.']);
        }
        else
        {
             return show(200,'更新成功',[]);
           // return response()->json(['code'=>200,'msg'=>'Success to do it.']);
        }
    }
//保存头像
    public function save_avatar()
    {
//        $data['id'] = intval(input('id'));

        $data['avatar'] = htmlspecialchars(Input::get('avatar'));
        $result = User::where(['id'=>$request->uid])->update($data);

        if(!$result)
        {
            return response()->json(['code'=>10212,'msg'=>'Failed to do it.']);
        }
        else
        {
            return response()->json(['code'=>200,'msg'=>'Success to do it.']);
        }
    }

    public function save_nickname()
    {
//        $data['id'] = intval(input('id'));

        $data['nickname'] = htmlspecialchars(Input::get('nickname'));

        if(trim($data['nickname']) == '')
        {
            return response()->json(['code'=>10211,'msg' => 'User is not exist.']);
        }

        $result =User::where(['id'=>$request->uid])->update($data);

        if(!$result)
        {
            return response()->json(['code'=>10212,'msg'=>'Failed to do it.']);
        }
        else
        {
            return response()->json(['code'=>200,'msg'=>'Success to do it.']);
        }
    }

    /**
     * 用户认证
     * @return mixed
     */
    public function verified()
    {




        return response()->json(['code'=>200,'msg'=>'Success.']);
    }


    /**
     * 我的收藏
     * @param string $ucid 用户唯一id
     */
    public function my_collect($ucid = '')
    {
        if(trim($ucid) == '' || strlen($ucid) != 32 )
        {
            echo json_encode(['code'=>10213,'msg'=>'User is not exist.']);exit;
        }

        $data = DB::table('wechatadmin.system_article')->alias('a')
            ->field('a.id,a.title,a.introduction,ac.create_time,a.cid')
            ->join('wechatadmin.system_article_collect ac','a.id=ac.aid')
            ->where(['ac.ucid'=>$ucid,'a.status'=>1])
            ->order('ac.create_time DESC')
            ->paginate(10,false,['query'=>request()->param()]);

        return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
    }

    /**
     * 我赞过的
     * @param string $ucid 用户唯一id
     */
    public function my_good($ucid = '')
    {
        if(trim($ucid) == '' || strlen($ucid) != 32 )
        {
            echo json_encode(['code'=>10213,'msg'=>'User is not exist.']);exit;
        }

        $data = DB::table('wechatadmin.system_article')->alias('a')
            ->field('a.id,a.title,a.introduction,ag.create_time,a.cid')
            ->join('wechatadmin.system_article_good ag','a.id=ag.aid')
            ->where(['ag.ucid'=>$ucid,'a.status'=>1])
            ->order('ag.create_time DESC')
            ->paginate(10,false,['query'=>request()->param()]);

        return response()->json(['code'=>200,'data'=>$data,'msg'=>'Success.']);
    }
}
